package com.zhanglijie.base.volatileLearn;

import java.util.ArrayList;

/**
 * @author zhanglijie
 * @version 1.0
 * @since 1.1.0 2022/5/15 0015 19:01
 */
public class RangeProblems {
    static int i= 0;
    static int count = 255;//大于255自动打破内存可见性
    public static void main(String[] args) {
        new Thread(()->{
            ArrayList<Integer> list = new ArrayList<>();
            for (int j = 1; j < count; j++) {
                list.add(j);
            }

            while(true){
                if(list.contains(i)){
                    System.out.println("数据存在");
                    break;
                }
            }
        },"thred0").start();

        new Thread(()->{
            mysleep(1000000000);//睡眠一秒
            for (int j = 1; j < count; j++) {
                i=j;
            }
        },"thread2").start();
    }

    /**
     * @param timeout 单位是纳秒
     */
    private static void mysleep(int timeout) {
        long start = System.nanoTime();
        long end;
        do{
            end = System.nanoTime();
        }while(end-start<=timeout);
    }
}
